package cs2340.g21.wms;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.util.Log;

/**
 * A class to filter lists of items based on various parameters
 * currently supports filter by: category, date
 * @author Alexander
 *
 */
public class Filter {
    private static final String LOG_TAG = "DEBUGGER";
    private List<Item> filteredItems;
    private List<Item> items;
    
    public Filter(List<Item> items){
        this.filteredItems = new ArrayList<Item>();
        this.items = items;
    }
    
    /**
     * sets filter to deal with a different list and resets the filtered list
     * @param items new list of items 
     */
    public void setItems(List<Item> items){
        this.items = items;
        this.filteredItems = new ArrayList<Item>();
    }
    
    /**
     * @return All LOST items from the input list
     */
    public List<Item> lostFilter(){
        if(items == null || items.size() == 0){
            Log.d(LOG_TAG, "null or empty heu");
            return filteredItems;
        }
        for(Item i : items){
            if(i.getCategory() == Category.LOST){
                Log.d(LOG_TAG, "filtered lost");
                filteredItems.add(i);
            }
        }
        return filteredItems;
    }
    
    /**
     * @return All FOUND items from the input list
     */
    public List<Item> foundFilter(){
        if(items == null || items.size() == 0){
            Log.d(LOG_TAG, "null or empty heu");
            return filteredItems;
        }
        for(Item i : items){
            if(i.getCategory() == Category.FOUND){
                Log.d(LOG_TAG, "filtered found");
                filteredItems.add(i);
            }
        }
        return filteredItems;
    }
   
    /**
     * @return All DONATE items from the input list
     */
    public List<Item> donateFilter(){
        if(items == null || items.size() == 0){
            Log.d(LOG_TAG, "null or empty heu");
            return filteredItems;
        }
        for(Item i : items){
            if(i.getCategory() == Category.DONATE){
                Log.d(LOG_TAG, "filtered donate");
                filteredItems.add(i);
            }
        }
        return filteredItems;
    }
    
    /**
     * @param day to which to limit the list
     * @param month to which to limit the list
     * @param year to which to limit the list
     * @return A list of all items entered before day/month
     */
    public List<Item> dateEarlierFilter(int day, int month, int year){
        if(items == null || items.size() == 0){
            Log.d(LOG_TAG, "null or empty heu");
            return filteredItems;
        }
        for(Item i : items){
            if(i.getDate().get(Calendar.YEAR) < year){
                filteredItems.add(i);
            }else if(i.getDate().get(Calendar.YEAR) == year){
                if(i.getDate().get(Calendar.MONTH) < month){
                    filteredItems.add(i);
                }else if(i.getDate().get(Calendar.MONTH) == month){
                    if(i.getDate().get(Calendar.DAY_OF_MONTH) <= day){
                        filteredItems.add(i);
                    }
                }
            }
        }
        return filteredItems;
    }
    
    /**
     * @param sDay starting day of the range
     * @param eDay ending day of the range
     * @param sMonth starting month of the range
     * @param eMonth ending month of the range
     * @param sYear starting year of the range
     * @param eYear starting year of the range
     * @return A list of all items entered from sDay/sMonth/sYear to eDay/eMonth/eYear
     */
    public List<Item> dateRangeFilter(int sDay, int eDay, int sMonth, int eMonth, int sYear, int eYear){
        if(items == null || items.size() == 0){
            Log.d(LOG_TAG, "null or empty heu");
            return filteredItems;
        }
        for(Item i : items){
            Log.d(LOG_TAG, "for loopin");
            //Log.d(LOG_TAG, "item date " + i.getDate().DAY_OF_MONTH + "." + i.getDate().MONTH + "." + i.getDate().YEAR);
            if(i.getDate().get(Calendar.YEAR) > sYear && i.getDate().get(Calendar.YEAR) < eYear){
                Log.d(LOG_TAG, "added year");
                filteredItems.add(i);
            }else if(i.getDate().get(Calendar.YEAR) == sYear || i.getDate().get(Calendar.YEAR) == eYear){
                Log.d(LOG_TAG, "eYear: " + eYear);
                if(i.getDate().get(Calendar.MONTH) > sMonth && i.getDate().get(Calendar.MONTH) < eMonth){
                    filteredItems.add(i);
                }
                else if(i.getDate().get(Calendar.MONTH) == sMonth || i.getDate().get(Calendar.MONTH) == eMonth){
                    if(i.getDate().get(Calendar.DAY_OF_MONTH) >= sDay && i.getDate().get(Calendar.DAY_OF_MONTH) <= eDay){
                        //Log.d(LOG_TAG, "added for day: " + sDay + "." + eDay + "/" + i.getDate().DAY_OF_MONTH);
                        filteredItems.add(i);
                    }
                }
            }
        }
        
        return filteredItems;
    }
    
    /**
     * @param day to which to limit the list
     * @param month to which to limit the list
     * @param year to which to limit the list
     * @return A list of all items entered after day/month
     */
    public List<Item> dateLaterFilter(int day, int month, int year){
        if(items == null || items.size() == 0){
            Log.d(LOG_TAG, "null or empty heu");
            return filteredItems;
        }
        for(Item i : items){
            if(i.getDate().get(Calendar.YEAR) > year){
                filteredItems.add(i);
            }else if(i.getDate().get(Calendar.YEAR) == year){
                if(i.getDate().get(Calendar.MONTH) > month){
                    filteredItems.add(i);
                }else if(i.getDate().get(Calendar.MONTH) == month){
                    if(i.getDate().get(Calendar.DAY_OF_MONTH) >= day){
                        filteredItems.add(i);
                    }
                }
            }
        }
        return filteredItems;
    }
}
